const cData = [];
for(let i=0;i<3;i++){
    cData.push('http://pic25.photophoto.cn/20121216/0010023993176540_b.jpg');
}

function delay(x){
    return new Promise((resolve,reject)=>{
        setTimeout(resolve,x);
    })
} 

let loadTimes = 3;

export default {

  namespace: 'pullLoad',

  state: {
      data:cData,
      hasMore: true,
      load:false,
      refresh:false
  },

  subscriptions: {
    setup({ dispatch, history }) {

    },
  },

  effects: {
    *fetchRemote({ payload }, { call, put, select }) {
    },
    *refresh({ payload }, { call, put, select }){
        loadTimes=3;
        const state = yield select(({pullLoad})=>pullLoad);
        yield put({type:'fetch',payload:{
            refresh:true
        }});
        yield call(delay,500);
        yield put({type:'fetch',payload:{
            refresh:false,
            data:cData,
            hasMore: true,
        }});
    },
    *loadMore({ payload }, { call, put, select }){
        const state = yield select(({pullLoad})=>pullLoad);
        yield put({type:'fetch',payload:{
            load:true
        }});
        yield call(delay,500);
        yield put({type:'addData'});
        yield put({type:'fetch',payload:{
            load:false,
            hasMore: loadTimes>0?true:false,
        }});
        loadTimes -= 1;
    }
  },

  reducers: {
    fetch(state, action) {
      return { ...state, ...action.payload };
    },
    addData(state, action){
        return {
            ...state,
            data: [...state.data,'http://pic25.photophoto.cn/20121216/0010023993176540_b.jpg']
        }
    }
  },

}
